package crafex.engine.system;

import crafex.engine.level.Level;
import crafex.engine.level.LevelHandler;
import crafex.engine.sound.SoundManager;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Crafex extends SurfaceView implements SurfaceHolder.Callback{
	
	public static App app;
	/**the gameloop*/
	private Systum systum;
	/**save info*/
	private SharedPreferences sharedPreferences;
	/**the phone screens width*/
	private static int screenWidth;
	/**the phone screens height*/
	private static int screenHeight;
	private static Level defaultLevel;
	/**the context?*/
	private final Context context;
	//TODO find out what the context is for
	
	/**
	 * 
	 */
	public static AssetManager assets;
	/**default paint for using*/
	public static Paint paint = new Paint();
	/**the command console for the game*/
	public static Console console = new Console();
	/**the sound manager*/
	public static SoundManager soundmanager = new SoundManager();
	/**the level magagment systum*/
	public static LevelHandler levelHandler;
	

	public Crafex(App app, Context context, SharedPreferences sharedPreferences, AssetManager assets, Level initLevel, int screenWidth, int screenHeight) {
		super(context);
		Crafex.app = app;
		this.context = context;
		this.sharedPreferences = sharedPreferences;
		Crafex.assets = assets;
		Crafex.screenWidth = screenWidth;
		Crafex.screenHeight = screenHeight;
		Crafex.defaultLevel = initLevel;
		levelHandler = new LevelHandler(initLevel);
		paint.setColor(Color.WHITE);
		getHolder().addCallback(this);
		init();		
	}
	
	public void init(){
		//TODO sound
		soundmanager.initSounds(context);
	}
	
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		systum = new Systum(this, 30);
        systum.startRunning();
        systum.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	
	public static void buttonInput(int keyCode){
		LevelHandler.getLevel().buttonHandler(keyCode);
	}
	
	/**
	 * the update for the game
	 */
	public void update(){
		
		if(levelHandler != null){
			levelHandler.update();
		}
		
	}

	/**
	 * the draw methods of the game
	 */
	public void draw(Canvas canvas){
		if(levelHandler != null){
			levelHandler.draw(canvas);
		}
	}
	
	
	/**
	 * gets the android screen width
	 * @return width
	 */
	public static int getScreenWidth(){
		return screenWidth;
	}
	
	/**
	 * gets the android screen height
	 * @return height
	 */
	public static int getScreenHeight(){
		return screenHeight;
	}
	
	/**
	 * gets the simple saving for the game
	 * @return
	 */
	public SharedPreferences getSharedPreferences() {
		return sharedPreferences;
	}
	
	public static Level getDefaultLevel() {
		return defaultLevel;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(levelHandler != null){
			levelHandler.updateInput(event);
		}
		return true;
	}
}